/*填空宽度控制*/
var textWidth = function(text){ 
    var sensor = $('<pre>'+ text +'</pre>').css({display: 'none'}); 
    $('body').append(sensor); 
    var width = sensor.width();
    sensor.remove();
    return width;
};
function changeText(obj){
    $(obj).width(textWidth($(obj).val())+30);
}

/* 创建编辑器  */
var E = window.wangEditor;
var editor = new E('#editor');
// 自定义菜单配置
editor.customConfig.menus = [
	'head',
    'emoticon'//加入空格
];
editor.create();

/* 下拉选择科目加载后台信息 */
$(function() {  
    $(".selectpicker").selectpicker({  
        noneSelectedText : '请选择'  
        });  
  
        $(window).on('load', function() {  
        $('.selectpicker').selectpicker('val', '');  
        $('.selectpicker').selectpicker('refresh');  
        });  
  
        // 下拉数据加载
    $.ajax({
        type : 'get',  
        url :"subject/findAll",  
        dataType : 'json',  
        success : function(datas) {// 返回list数据并循环获取
            var select = $("#slpk");  
            for (var i = 0; i < datas.length; i++) {  
                select.append("<option value='"+datas[i].id+"'>"  
                        + datas[i].name + "</option>");  
            }  
            $('.selectpicker').selectpicker('val', '');  
            $('.selectpicker').selectpicker('refresh'); 
        }  
    });
});
/* 单选题信息提交 */
$("#blankForm").submit(function(event){
	var blank = {};
	var blanks = {};
	
	var id = $('#id').val();
	var num = $('#num').val();
	$(blank).attr("id",id);
	$(blank).attr("num",num);
	
	var subject = {};
	var subjectId = $('#slpk option:selected').val();
	$(subject).attr("id",subjectId);
	$(blank).attr("subject",subject);
	
	var inputs = $('.the-blank');
	for(var i=0;i<inputs.length;i++){
		var option = {};
		$(option).attr("content",inputs[i].value);
		$(blanks).attr((i+1),option);
	}
	$(blank).attr("blanks",blanks);
	
	var content = editor.txt.html();
	if(content==null||content==""||content=="<p><br></p>"){
		alert("题干不能为空！");
		return false;
	}
	$(blank).attr("content",content);
	
	var result = JSON.stringify(blank);
	
	$.ajax({
		url:'blank/saveOrUpdate',
		type:'post',
		dataType:'json',
		data:{blank:result},
		success:function(data){
			alert(data.msg);
			if(data.success)
				window.location.reload();
		},
		error:function(){
			alert("出错！");
		}
	});
	
	return false;
});
function test(){
	var blank = {};
	var blanks = {};
	
	var id = $('#id').val();
	var num = $('#num').val();
	
	var inputs = $('.the-blank');
	for(var i=0;i<inputs.length;i++){
		$(blanks).attr((i+1),inputs[i].value);
	}
	console.log(JSON.stringify(blanks));
	
	
	
	var content = editor.txt.html();
	console.log(content);
	$("body").append(content);
	alert($('#slpk option:selected').val());
}